package ua.at.tsvetkov.webservices;

import ua.at.tsvetkov.application.AppConfig;
import ua.at.tsvetkov.util.Log;

/**
 * URI abstract constructor.
 * 
 * @author Alexandr Tsvetkov 26.09.2013
 */
public abstract class UrlsConstructor {

	private static String		serverRoot	= null;
	public static final String	LOCAL			= "file://";
	public static final String	AMP			= "&";

	protected static UrlBuilder getUrlBuilder() {
		return new UrlBuilder(serverRoot);
	}

	/**
	 * Build string with StringBuilder. Simple concatenate all strings.
	 * 
	 * @param str strings for concatenate
	 * @return concatenated string
	 */
	protected static String buildString(String... str) {
		StringBuilder sb = new StringBuilder();
		for (String string : str) {
			sb.append(string);
		}
		return sb.toString();
	}

	/**
	 * Get Server Root url. If url is't assigned then throw NullPointerException.
	 * 
	 * @return
	 */
	public static String getServerRoot() {
		if (serverRoot == null || serverRoot.equals(""))
			throw new NullPointerException("The Server Root url is not defined. Plese specify it by setServerRoot(String root) method");
		return serverRoot;
	}

	/**
	 * Set Server Root url
	 * 
	 * @param root
	 */
	public static void setServerRoot(String root) {
		serverRoot = root;
		Log.v("New server root: " + serverRoot);
	}

	/**
	 * Construct http request string with default server root string
	 * 
	 * @param request strings like "register?user=", "tom"
	 * @return url like "http://yourserver.com/register?user=tom"
	 */
	protected static String request(String... request) {
		StringBuilder sb = new StringBuilder();
		sb.append(getServerRoot());
		for (String string : request) {
			sb.append(string);
		}
		return showLog(sb.toString().replace(" ", "%7C").replace("\n", "%7C"));
	}

	/**
	 * Construct to local file system path string with default directory {@link AppConfig#getDir()}
	 * 
	 * @param request
	 * @return
	 */
	protected static String pathLocal(String... strs) {
		StringBuilder sb = new StringBuilder();
		sb.append(AppConfig.getDir());
		for (String string : strs) {
			sb.append(string);
		}
		return showLog(sb.toString());
	}

	/**
	 * Construct to local file system request string with default directory {@link AppConfig#getDir()}
	 * 
	 * @param request
	 * @return
	 */
	protected static String requestLocal(String... request) {
		StringBuilder sb = new StringBuilder();
		sb.append(LOCAL);
		sb.append(AppConfig.getDir());
		for (String string : request) {
			sb.append(string);
		}
		return showLog(sb.toString());
	}

	private static String showLog(String url) {
		Log.v("-> CALL URI " + url);
		return url;
	}

}
